/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openbrew.openbrewrf;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author dchapman
 */
@Entity
@Table(name = "InvSalesLines")
@XmlRootElement
public class InvSalesLines implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "iSaleID")
    private Integer invSaleID;
    @Basic(optional = false)
    @NotNull
    @Column(name = "iSaleDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date invSaleDate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "iSaleQty")
    private int invSaleQty;
    @Basic(optional = false)
    @NotNull
    @Column(name = "iSalePriceOver")
    private float invSalePriceOver;
    @Basic(optional = false)
    @NotNull
    @Column(name = "iSaleUnitPrice")
    private float iSaleUnitPrice;
    @JoinColumn(name = "iSaleCustID", referencedColumnName = "CId")
    @ManyToOne(optional = false)
    private Customer invSaleCustID;
    @JoinColumn(name = "iSaleSupplyID", referencedColumnName = "SupID")
    @ManyToOne(optional = false)
    private Supplies invSaleSupplyID;
    @JoinColumn(name = "iSaleDisc", referencedColumnName = "DiscID", nullable=true)
    @ManyToOne(optional = false)
    private DiscountCode invSaleDisc;

    public InvSalesLines() {
    }

    public InvSalesLines(Integer invSaleID) {
        this.invSaleID = invSaleID;
    }

    public InvSalesLines(Integer invSaleID, Date invSaleDate, int invSaleQty, float invSalePriceOver) {
        this.invSaleID = invSaleID;
        this.invSaleDate = invSaleDate;
        this.invSaleQty = invSaleQty;
        this.invSalePriceOver = invSalePriceOver;
    }

    public Integer getInvSaleID() {
        return invSaleID;
    }

    public void setInvSaleID(Integer invSaleID) {
        this.invSaleID = invSaleID;
    }

    public Date getInvSaleDate() {
        return invSaleDate;
    }

    public void setInvSaleDate(Date invSaleDate) {
        this.invSaleDate = invSaleDate;
    }

    public int getInvSaleQty() {
        return invSaleQty;
    }

    public void setInvSaleQty(int invSaleQty) {
        this.invSaleQty = invSaleQty;
    }

    public float getInvSalePriceOver() {
        return invSalePriceOver;
    }

    public void setInvSalePriceOver(float invSalePriceOver) {
        this.invSalePriceOver = invSalePriceOver;
    }

    public Customer getInvSaleCustID() {
        return invSaleCustID;
    }

    public void setInvSaleCustID(Customer invSaleCustID) {
        this.invSaleCustID = invSaleCustID;
    }

    public Supplies getInvSaleSupplyID() {
        return invSaleSupplyID;
    }

    public void setInvSaleSupplyID(Supplies invSaleSupplyID) {
        this.invSaleSupplyID = invSaleSupplyID;
    }

    public DiscountCode getInvSaleDisc() {
        return invSaleDisc;
    }

    public void setInvSaleDisc(DiscountCode invSaleDisc) {
        if(invSaleDisc.getDiscID()!=null){
            this.invSaleDisc = invSaleDisc;
        }
    }

    public float getiSaleUnitPrice() {
        return iSaleUnitPrice;
    }

    public void setiSaleUnitPrice(float iSaleUnitPrice) {
        this.iSaleUnitPrice = iSaleUnitPrice;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (invSaleID != null ? invSaleID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Sales)) {
            return false;
        }
        InvSalesLines other = (InvSalesLines) object;
        if ((this.invSaleID == null && other.invSaleID != null) || (this.invSaleID != null && !this.invSaleID.equals(other.invSaleID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.openbrew.openbrewrf.InvSalesLines[ iSaleID=" + invSaleID + " ]";
    }

    /***********************************************************************
     * Standard display for pricing and discount text, etc.
     **********************************************************************/
    /***********************************************************************
     * Get Line Price (returns line item price based on settings of line)
     * @return Price
     **********************************************************************/
    public double getLinePrice(){
        double price = (double)this.getiSaleUnitPrice();
        if(this.getInvSalePriceOver()>0){
            price = (double)this.getInvSalePriceOver();
        }
        if(this.getInvSaleDisc()!=null){
            price = price - (price * (double)this.getInvSaleDisc().getDiscNum());
        } 
        return price;
    }

    /***************************************************************************
     * Get Sales Line's subtotal based on settings.
     * @return 
     **************************************************************************/
    public double getLineSubTot(){
        final double subTot = getLinePrice() * (double)this.getInvSaleQty();
        return subTot;
    }

    /***************************************************************************
     * Get the appropriate discount description based on settings.
     * @return 
     **************************************************************************/
    public String getDiscountText(){
        if(this.getInvSalePriceOver()>0){
            return "Price Override";
        } else if (this.getInvSaleDisc()!=null) {
            return this.getInvSaleDisc().getDiscName();
        } else {
            return "No Discount";
        }
    }

    /***************************************************************************
     * Get String in CSV format for displaying sales line information.
     * @return 
     **************************************************************************/
    public String getSalesString(){
        final NumberFormat money = new DecimalFormat("$#0.00");
        String str = "";
        final int numUnits = this.getInvSaleQty();
        str += this.getInvSaleSupplyID().getDisplay();
        str +="," + money.format(getLinePrice());
        str +="," + Integer.toString(numUnits);
        str +="," + money.format(getLineSubTot()) + "\n";
        return str;
    }
}
